package com.betterjr.modules.application.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.application.dao.AmsResourceMapper;
import com.betterjr.modules.application.data.AmsMenuResourceRelationConstantCollections;
import com.betterjr.modules.application.data.ApplicationGroupConstantCollections;
import com.betterjr.modules.application.entity.AmsResource;
import com.betterjr.modules.application.entity.AmsResourceExtend;

@Service
public class AmsResourceService extends BaseService<AmsResourceMapper, AmsResource> {

    /**
     * 
     * @Title: queryResources 
     * @Description: 根据状态查询资源信息
     * @param anBusinStatus  状态  1 生效  9 不生效
     * @return 根据状态查询资源信息
     * @throws 
     * @author xuyp
     * @date 2017年12月20日 上午10:29:35
     */
    @SuppressWarnings("unchecked")
    public List<AmsResource> queryResources(String anBusinStatus) {

        Map<String, Object> map = QueryTermBuilder.newInstance().build();
        if (StringUtils.isBlank(anBusinStatus)) {
            anBusinStatus = AmsMenuResourceRelationConstantCollections.AMSMENURESOURCERELATION_BUSINSTATUS_EFFECTIVE;
        }

        if (anBusinStatus.contains(ApplicationGroupConstantCollections.SYSTEM_VALUE_FILE_SEPARATOR)) {

            map.put("businStatus",
                    anBusinStatus.split(ApplicationGroupConstantCollections.SYSTEM_VALUE_FILE_SEPARATOR));
        } else {
            map.put("businStatus", anBusinStatus);
        }

        return this.selectByProperty(map, "applicationGroupName");

    }

    // @SuppressWarnings("unchecked")
    /**
     * 
     * @Title: queryResourcesExtends 
     * @Description: 根据状态查询资源信息并且封装成新增菜单的时候使用
     * @param anBusinStatus  状态  1 生效  9 不生效
     * @return 根据状态查询资源信息并且封装成新增菜单的时候使用
     * @throws 
     * @author xuyp
     * @date 2017年12月20日 上午10:30:19
     */
    public Map<String, List<AmsResourceExtend>> queryResourcesExtends(String anBusinStatus) {

        List<AmsResource> queryResources = queryResources(anBusinStatus);
        HashMap<String, List<AmsResourceExtend>> hashMap = new HashMap<String, List<AmsResourceExtend>>();
        if (Collections3.isEmpty(queryResources)) {
            return hashMap;
        }

        for (AmsResource amsResource : queryResources) {

            AmsResourceExtend resourceExtend = convertToExtend(amsResource);
            if (hashMap.containsKey(amsResource.getApplicationGroupName())) {
                hashMap.get(amsResource.getApplicationGroupName()).add(resourceExtend);
            } else {
                List<AmsResourceExtend> list = new ArrayList<>();
                list.add(resourceExtend);
                hashMap.put(amsResource.getApplicationGroupName(), list);
            }
        }

        return hashMap;

    }

    /**
     * 
     * @Title: saveOrUpdateResource 
     * @Description: 新增资源信息
     * @param anResource
     * @return 新增资源信息
     * @throws 
     * @author xuyp
     * @date 2017年12月20日 上午10:31:10
     */
    public AmsResource saveOrUpdateResource(AmsResource anResource) {

        BTAssert.notNull(anResource, "数据为空,操作失败");
        anResource.setBusinStatus(
                AmsMenuResourceRelationConstantCollections.AMSMENURESOURCERELATION_BUSINSTATUS_EFFECTIVE);
        List<AmsResource> list = this.select(anResource);
        if (!Collections3.isEmpty(list)) {
            for (AmsResource amsResource : list) {
                amsResource.setBusinStatus(
                        AmsMenuResourceRelationConstantCollections.AMSMENURESOURCERELATION_BUSINSTATUS_NOEFFECTIVE);
                this.updateByPrimaryKeySelective(amsResource);
            }
        }

        this.insertSelective(anResource);

        return anResource;
    }

    private AmsResourceExtend convertToExtend(AmsResource amsResource) {

        AmsResourceExtend extend = amsResource.conventToExtend();
        return extend;
    }

}
